package v1

import (
	"log"
	"math/rand"
	v4 "mindonmap/api/v4"
	"mindonmap/pkgs/e"
	"mindonmap/pkgs/setting"
	"mindonmap/pkgs/util"
	"net/http"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
	"mindonmap/validation"

	"mindonmap/models"
)

// 获取脑图分享列表
func GetMindShares(c *gin.Context) {
	data := make(map[string]interface{})
	var maps models.MindShare
	valid := validation.Validation{}

	if err := c.BindJSON(&maps); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.INVALID_PARAMS,
			"msg":  e.GetMsg(e.INVALID_PARAMS),
			"data": make(map[string]interface{}),
		})
		return
	}
	Userid, err := v4.GetUserID(c)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT,
			"msg":  e.GetMsg(e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT),
			"data": make(map[string]interface{}),
		})
		return
	}
	code := e.INVALID_PARAMS
	if !valid.HasErrors() {
		code = e.SUCCESS
		data["lists"] = models.GetMindShares(util.GetPage(c), setting.PageSize, Userid)
	} else {
		for _, err := range valid.Errors {
			log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": data,
	})
}

// 新增脑图分享
func AddMindShare(c *gin.Context) {
	var json models.MindShare
	if err := c.BindJSON(&json); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.INVALID_PARAMS,
			"msg":  e.GetMsg(e.INVALID_PARAMS),
			"data": make(map[string]interface{}),
		})
		return
	}
	Userid, err := v4.GetUserID(c)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT,
			"msg":  e.GetMsg(e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT),
			"data": make(map[string]interface{}),
		})
		return
	}
	code := e.INVALID_PARAMS
	data := make(map[string]interface{})

	YesNo := models.ExistMindMapAndFlowByID(json.Pid, json.Types)
	if !YesNo {
		c.JSON(http.StatusOK, gin.H{
			"code": e.ERROR_NOT_EXIST_ARTICLE,
			"msg":  e.GetMsg(e.ERROR_NOT_EXIST_ARTICLE),
			"data": data,
		})
		return
	}
	if !models.ExistMindShareByMindID(json.Pid) {
		now := time.Now()
		nowtime := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, 7)
		data["pid"] = json.Pid
		data["acid"] = Userid
		if json.Types == "f" {
			data["url"] = setting.GetShareUrl("f") + json.Shareid
		} else {
			data["url"] = setting.GetShareUrl("m") + json.Shareid
		}
		data["checktype"] = json.Checktype
		data["expires"] = nowtime.Unix()
		randomnumber := rand.New(rand.NewSource(time.Now().UnixNano())).Int31n(10000)
		rans := strconv.Itoa(int(randomnumber))
		data["pass"] = rans
		data["state"] = 0
		data["shareid"] = json.Shareid
		data["types"] = json.Types
		models.AddMindShare(data)
		updatedata := make(map[string]interface{})
		updatedata["sharestate"] = 1
		if json.Types == "m" {
			models.EditMindMap(json.Pid, updatedata)
		}
		if json.Types == "f" {
			models.EditMindFlow(json.Pid, updatedata)
		}

		code = e.SUCCESS
	} else {
		res := models.GetMindShareID(json.Pid)
		data["pid"] = res.Pid
		data["acid"] = res.Acid
		data["url"] = res.Url
		data["checktype"] = res.Checktype
		data["expires"] = res.Expires
		data["pass"] = res.Pass
		data["shareid"] = res.Shareid
		code = e.SUCCESS
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": data,
	})
}

// 编辑脑图分享
func EditMindShare(c *gin.Context) {
	var json models.MindShare
	if err := c.BindJSON(&json); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.INVALID_PARAMS,
			"msg":  e.GetMsg(e.INVALID_PARAMS),
			"data": make(map[string]interface{}),
		})
		return
	}
	code := e.INVALID_PARAMS
	data := make(map[string]interface{})
	if models.ExistMindShareByID(json.Shareid) {

		data["checktype"] = json.Checktype
		data["expires"] = json.Expires
		data["modifietime"] = time.Now().Unix()
		models.EditMindShare(json.Shareid, data)
		code = e.SUCCESS
	} else {
		code = e.ERROR_NOT_EXIST_ARTICLE
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": data,
	})
}

// 取消脑图分享
func DeleteMindShare(c *gin.Context) {
	var json models.MindShare
	if err := c.BindJSON(&json); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.INVALID_PARAMS,
			"msg":  e.GetMsg(e.INVALID_PARAMS),
			"data": make(map[string]interface{}),
		})
		return
	}

	valid := validation.Validation{}
	valid.Required(json.Shareid, "shareid").Message("不能为空")
	valid.Required(json.Pid, "pid").Message("不能为空")

	code := e.INVALID_PARAMS
	if !valid.HasErrors() {
		if models.ExistMindShareByID(json.Shareid) {
			data := make(map[string]interface{})
			data["state"] = 1
			data["statetime"] = time.Now().Unix()
			models.DeleteMindShare(json.Shareid, data)
			updatedata := make(map[string]interface{})
			updatedata["sharestate"] = 0

			if json.Types == "m" {
				models.EditMindMap(json.Pid, updatedata)
			}
			if json.Types == "f" {
				models.EditMindFlow(json.Pid, updatedata)
			}
			code = e.SUCCESS
		} else {
			code = e.ERROR_NOT_EXIST_ARTICLE
		}
	} else {
		for _, err := range valid.Errors {
			log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": make(map[string]string),
	})
}
